(function(){
	angular.module('webApp').controller("FavoritesCtrl",FavoritesCtrl);
	FavoritesCtrl.$inject=['$scope','helper','$rootScope','$timeout','dataService'];
	function FavoritesCtrl($scope,helper,$rootScope,$timeout,dataService){
		$scope.init=function(){
		$scope.tabIndex=1;//默认显示tab页面
		$scope.editStatus=false;//编辑是否开启状态
		$scope.favoritesId={};
			var tab = new auiTab({
	            element:document.getElementById("favoritestab"),
	            callback:function(o,dom){
	            }
	        },function(ret){
	        	$scope.tab(ret)
	        });
		}
		$scope.tab=function(ret){
			$timeout(function(){
	         	$scope.tabIndex=ret.index;
			})
		}
		$scope.favoritesGet=function(){
			$rootScope.FavoritesGet().then(function(r){
				$scope.favorites = r.Favorites;
				_.map($scope.favorites,function(favorite){
					dataService.itemOne(favorite.item_id).then(function(items){
						favorite.item=items;
						var price=[];
						async.map(favorite.item.skus,function(sku,callback){
							price.push(sku.sku_price);
							callback(null,null);
						},function(e,r){
							favorite.price=_.min(price);
						});

					})
				});
			});
		}
		$scope.favoritesDelete=function(){
			_.map($scope.favoritesId,function(id){
				dataService.favoritesDelete(id).then(function(r){
					if(r.Msg){
						helper.myAlert("error:"+r.Msg);
					}else{
						$scope.editStatus=false;
						$scope.favoritesGet();
					}
				});
			});
		}
		$scope.favoritesCheckboxDeleteId=function(favorite){
			if(!$scope.favoritesId[favorite.fav_id]==favorite.deleteCheckbox){
				$scope.favoritesId[favorite.fav_id]=favorite.fav_id;
			}else{
				delete $scope.favoritesId[favorite.fav_id];
			}

		}
		$scope.EditStatus=function(){
			if($scope.editStatus==false){
				$scope.editStatus=true;
			}else{
				$scope.editStatus=false;
			}
		}
		
	}
})();